package org.nhindirect.stagent;

import java.util.Collection;

import javax.mail.internet.InternetAddress;

import org.nhindirect.stagent.trust.TrustEnforcementStatus;

public class OutgoingMessage extends NHINDMessage 
{
    NHINDAddressCollection m_recipients;
    NHINDAddressCollection m_rejectedRecipients;

    public OutgoingMessage()
    {
    }

    public OutgoingMessage(MimeEntity entity)
    {
    	this.setHeaders(entity.getHeaders());
    	this.setBody(entity.getBody());
    }    
    
    @Override
    public Header getTo()
    {
    	return super.getTo();
    }
        
    public void setTo(Header value)
    {
        super.setTo(value);
        this.m_recipients = null;
    }
    
    public NHINDAddressCollection getRecipients()
    { 	
        if (this.m_recipients == null)
        {
        	this.m_recipients = new NHINDAddressCollection();
        	
        	Collection<InternetAddress> addrs =  this.toAddressCollection(this.getTo());
        	for (InternetAddress addr : addrs)
        		this.m_recipients.add(new NHINDAddress(addr));     
        }
        
        return this.m_recipients;
    }
    
    public boolean hasRecipients()
    {

    	Collection<NHINDAddress>recipients = this.getRecipients();
            return (recipients != null && recipients.size() > 0);
    }
    
    public NHINDAddressCollection getRejectedRecipients()
    {
        if (this.m_rejectedRecipients == null)
        {
            this.m_rejectedRecipients = new NHINDAddressCollection();
        }
        
        return this.m_rejectedRecipients;
    }
    
    public boolean hasRejectedRecipients()
    {

            return (this.m_rejectedRecipients != null && this.m_rejectedRecipients.size() > 0);
    }

    void categorizeRecipients(TrustEnforcementStatus minTrustStatus)
    {
        this.m_rejectedRecipients = NHINDAddressCollection.create(this.getRecipients().getUntrusted(minTrustStatus));
        this.getRecipients().removeUntrusted(minTrustStatus);            
    }
    
    void updateTo()
    {
        this.setParsedTo(this.getRecipients().toInternetAddressCollection());
    }

}
